<?php

namespace App\Admin\Controllers;

use App\Admin\Renderable\ArticleTable;
use App\Admin\Renderable\UserTable;
use App\Admin\Repositories\ArticleComment;
use App\Models\Article;
use App\Models\User;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class ArticleCommentController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new ArticleComment(['user:id,nickname,avatar','article:id,title']), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->column('user.avatar','用户头像')->image('',80,80);
            $grid->column('user.nickname','用户昵称');
            $grid->column('article.title','所属文章');
            $grid->column('audit_status')
                ->using([1 => '待审核',2 =>'已通过',3 => '已拒绝'])
                ->label([1 => 'danger',2 => 'success',3 => 'warning']);
            $grid->column('created_at');
            $grid->column('updated_at')->sortable();
            $grid->fixColumns(1);

            $grid->filter(function (Grid\Filter $filter) {
                $filter->panel();
                $filter->equal('user.nickname','用户昵称')->width(3);
                $filter->equal('audit_status')
                       ->select([1 => '待审核',2 =>'已通过',3 => '已拒绝'])
                      ->width(3);

            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new ArticleComment(), function (Show $show) {
            $show->field('id');
            $show->field('parent_id');
            $show->field('article_id');
            $show->field('user_id');
            $show->field('content');
            $show->field('image');
            $show->field('audit_status');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new ArticleComment(), function (Form $form) {
            $form->display('id');

            $form->selectTable('user_id','用户')
                 ->from(UserTable::make())
                 ->model(User::class,'id','nickname')
                 ->required();

            $form->selectTable('article_id','文章')
                ->from(ArticleTable::make())
                ->model(Article::class,'id','title')
                ->required();
            $form->image('image')
                ->disk('oss')
                ->autoUpload()
                ->removable(false)
                ->uniqueName()
                ->saveFullUrl()
                ->saving(function ($value){
                    return (string)$value;
                });
            $form->textarea('content')
                 ->required();


            $form->radio('audit_status')
                ->options([1 => '待审核',2 => '已通过',3 => '已拒绝'])
                ->required();


            $form->saved(function (Form $form){
                if ($form->input('user_id') && $form->input('audit_status')==2){
                    event(new \App\Events\Message([
                        'user_id' =>  $form->input('user_id'),
                        'type' => 2,
                        'content' => '恭喜您发布的文章评论已通过审核'
                    ]));
                }

                if ($form->input('user_id') && $form->input('audit_status')==3){
                    event(new \App\Events\Message([
                        'user_id' =>  $form->input('user_id'),
                        'type' => 2,
                        'content' => '您发布的文章评论未通过审核'
                    ]));
                }

            });

            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
